const SERVICE = require("../../lib/js/service.js");

Page({
  data:{
    pickerVal: [0, 0],    //picker下标
    showPupop: false,   //显示城市 
    Date: [[],[]],    
    date: null,
  },
  onLoad(){
    // this.Date(2013,2018);
    this.City();
  },
  
  onShow(options){
    if(options){
      this.setData({
        onShowOptions: options
      });
      this.Date(options.min_reg_year, options.max_reg_year);
    }
  },

  /**分享**/
  onShareAppMessage(){

  },

  
   Date(a,n){
     let td = this.data;
     for (let i = a; i <= n; i++) {
       td.Date[0].push(i);
     }
     for(let e = 1; e <= 12; e++){
       td.Date[1].push(e);
     }
     this.setData({
       Date: td.Date
     });
   },

  /**
   * @explain getCity 获取城市
   * 
   * **/
  getCity(){
    this.setData({
      showPupop: true
    });
  },
  City(){
    let _this = this;
    SERVICE.POST(SERVICE.URL.getAllCity,{},(status,res)=>{
      console.log("获取城市:",res);
      if(res.data.status === "success"){
        _this.setData({
          city: res.data.result.city_list
        });
      }else{
        wx.showToast({
          title: "获取城市失败",
          icon: "none",
          mask: true
        });
      }
    });
  },

  /*城市*/
  pickerChange(e) {
    let td = this.data;
    if (e.detail.value[0] != td.pickerVal[0]) {
      td.pickerVal[1] = 0;
      td.pickerVal[0] = e.detail.value[0];
    } else {
      td.pickerVal[1] = e.detail.value[1];
    }

    this.setData({
      pickerVal: td.pickerVal
    });
  },

  /*取消*/
  pupopCancel() {
    this.setData({
      showPupop: false
    });
  },

  /*确定*/
  pupopConfirm() {
    let td = this.data, city = td.city, pickerVal = td.pickerVal;
      this.setData({
        showPupop: false,
        get_city: city[pickerVal[0]].list[pickerVal[1]].city_name,
        city_name: city[pickerVal[0]].prov + city[pickerVal[0]].list[pickerVal[1]].city_name,
      });
  },

  /*获取日期*/
  getDate(e){
    let td = this.data, indexs = e.detail.value;
    this.setData({
      date: td.Date[0][indexs[0]] + '-' + td.Date[1][indexs[1]]
    });
  },

  formSubmit(e){
    let _this = this, td = this.data,
    form_obj = e.detail.value, id = e.detail.target.dataset.id;
    // if (!form_obj.vehiclemodel){
    //   wx.showToast({
    //     title: "请选择车型号",
    //     icon: "none",
    //     mask: true
    //   });
    // }
    if (!td.onShowOptions){
      wx.showToast({
        title: "请选择车型号",
        icon: "none",
        mask: true
      });
    }else if(!form_obj.city){
      wx.showToast({
        title: "请选所在城市",
        icon: "none",
        mask: true
      });
    } else if (!form_obj.date) {
      wx.showToast({
        title: "请选上牌时间",
        icon: "none",
        mask: true
      });
    } else if (!form_obj.mileage) {
      wx.showToast({
        title: "请输入里程",
        icon: "none",
        mask: true
      });
    }else{
      // let params = {
      //   modelId: td.onShowOptions.modelId,
      //   regDate: form_obj.date,
      //   mile: form_obj.mileage,
      //   zone: id
      // };
      td.onShowOptions.regDate = form_obj.date;
      td.onShowOptions.mile = form_obj.mileage;
      td.onShowOptions.zone = id;
      td.onShowOptions.city_name = td.city_name;
      wx.navigateTo({
        url: "../vehicle-valuation-detail/vehicle-valuation-detail?params=" + JSON.stringify(td.onShowOptions),
      });
     
    }
  }
});

